package com.dartfar.workflow.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.base.model.Person;
import com.dartfar.base.model.Role;
import com.dartfar.base.service.PersonService;
import com.dartfar.base.service.RoleService;
import com.dartfar.util.EntityUtils;
import com.dartfar.util.StringUtils;
import com.dartfar.workflow.constant.WorkflowCaleType;
import com.dartfar.workflow.service.WorkflowPermissionService;
import com.dartfar.workflow.vo.WorkflowEntrustVo;

/**
 * 流程权限Action
 * @author 王琦
 *
 */
@Scope("prototype")
@Component("workflowPermissionAction")
public class WorkflowPermissionAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(WorkflowPermissionAction.class);
	
	@Resource
	private WorkflowPermissionService workflowPermissionService;
	@Resource
	private RoleService roleService;
	@Resource
	private PersonService personService;
	
	/**
	 * 传递参数
	 */
	private String assigneType;
	private String assigneRoles;
	private String assignees;
	
	/**
	 * 加载流程按钮数据
	 * @return
	 */
	public String loadWorkflowBtnData(){
		try{
			JSONObject returnObj = new JSONObject();
			workflowPermissionService.loadWorkflowPermissionData(curPerson(), modelName, modelIdName, id, returnObj);
			logger.info("jsonResult: " + returnObj.toString());
			response.getWriter().write(returnObj.toString());
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 加载工作流的角色和人员
	 * @return
	 * @throws IOException 
	 */
	public String loadWorkflowUserRoleData() throws IOException{
		
		String jsonResult = "";
		//如果委托角色类型不为空
		if(StringUtils.isNotEmpty(assigneType)){
			List<WorkflowEntrustVo> workflowEntrustVos = new ArrayList<WorkflowEntrustVo>(); 
			//如果委派类型是角色 并且 委派角色不能为空
			if(assigneType.equalsIgnoreCase(WorkflowCaleType.CALE_ROLE) && StringUtils.isNotEmpty(assigneRoles)){
				assigneRoles = assigneRoles.replace(",", "','");
				//取得角色列表
				List<Role> roles = roleService.getListByIds(assigneRoles);
				//如果角色列表不为空 并且 角色数量 大于 零
				if(EntityUtils.isNotNull(roles) && roles.size() > 0){
					//遍历角色
					for(Role r : roles){
						//取得人员列表
						List<Person> persons = r.getPersons();
						//遍历人员
						for(Person p : persons){
							WorkflowEntrustVo wev = new WorkflowEntrustVo();
							wev.setId(String.valueOf(p.getPersonId()));
							wev.setName(p.getName());
							wev.setDepartName(p.getDepart().getName());
							wev.setRoleName(r.getName());
							workflowEntrustVos.add(wev);
						}
					}
					jsonResult = JSONArray.fromObject(workflowEntrustVos).toString();
				}else{
					jsonResult = JSONArray.fromObject(workflowEntrustVos).toString();
				}
				//如果委托人类型是候选人 或者果委托人类型是委派执行人 并且 果委托人不为空
			}else if((assigneType.equalsIgnoreCase(WorkflowCaleType.CALE_USER) || assigneType.equalsIgnoreCase(WorkflowCaleType.CALE_ASSIGNE))  && StringUtils.isNotEmpty(assignees)){
				assignees = assignees.replace(",", "','");
				//取得人员列表
				List<Person> persons = personService.getListByIds(assignees);
				//如果人员不为空 并且 人员数量 大于 零
				if(EntityUtils.isNotNull(persons) && persons.size() > 0){
					//遍历人员
					for(Person p : persons){
						WorkflowEntrustVo wev = new WorkflowEntrustVo();
						wev.setId(String.valueOf(p.getPersonId()));
						wev.setName(p.getName());
						wev.setDepartName(p.getDepart().getName());
						//wev.setRoleName(p.getRoles().g);
						workflowEntrustVos.add(wev);
					}
					jsonResult = JSONArray.fromObject(workflowEntrustVos).toString();
				}else{
					jsonResult = JSONArray.fromObject(workflowEntrustVos).toString();
				}
			}else{
				jsonResult = JSONArray.fromObject(workflowEntrustVos).toString();
			}
		}
		response.getWriter().write(jsonResult);
		return null;	
	}

	
	
	public String getAssigneType() {
		return assigneType;
	}

	public void setAssigneType(String assigneType) {
		this.assigneType = assigneType;
	}

	public String getAssigneRoles() {
		return assigneRoles;
	}

	public void setAssigneRoles(String assigneRoles) {
		this.assigneRoles = assigneRoles;
	}

	public String getAssignees() {
		return assignees;
	}

	public void setAssignees(String assignees) {
		this.assignees = assignees;
	}
	
	
	

}
